from PyQt6.QtCore import QFileInfo, Qt
from PyQt6.QtWidgets import QTreeWidget, QTreeWidgetItem, QPushButton, QWidget, QHBoxLayout, QVBoxLayout, QTextEdit, \
    QLabel, QFileDialog, QRadioButton, QButtonGroup, QGridLayout, QLineEdit


class SyncToolsWindow(QWidget):

    def __init__(self, text: str, parent=None):
        super().__init__(parent)

        self.setObjectName(text)

        # 窗口是上下两层布局
        # 第一层：文件位置选择，一个源文件，多个同步位置
        # 第二层：文件同步进度，分别展示多个同步的进度
        self.setWindowTitle('文件同步')

        self.fileMode = QFileDialog.FileMode.AnyFile

        # 总体布局 垂直布局
        self.mainLay = QVBoxLayout(self)
        self.mainLay.setContentsMargins(0, 0, 0, 0)
        self.mainLay.setSpacing(50)
        self.mainLay.setAlignment(Qt.AlignmentFlag.AlignTop)

        # 文件同步模式
        self.init_file_mode()

        # 源文件部分
        self.init_org_file()

        # 备份位置部分
        self.init_backup_file()

        self.mainLay.addWidget(self.fileModeView)
        self.mainLay.addWidget(self.orgFileSource)
        self.mainLay.addWidget(self.backUpFileSource)

        self.setLayout(self.mainLay)

    def init_file_mode(self):
        # 文件模式选择
        self.fileModeView = QWidget()
        self.fileModeView.setWindowTitle('源文件')
        # 模式选择按钮
        # 创建单选按钮组
        self.button_group = QButtonGroup()
        radio_button1 = QRadioButton("同步文件")
        radio_button2 = QRadioButton("同步文件夹")
        # 将单选按钮添加到按钮组
        self.button_group.addButton(radio_button1)
        self.button_group.addButton(radio_button2)

        gLay = QGridLayout()
        # 设置默认选中的按钮
        radio_button1.setChecked(True)
        gLay.setAlignment(Qt.AlignmentFlag.AlignLeft)
        gLay.addWidget(QLabel('文件同步模式：'), 0, 0)
        gLay.addWidget(radio_button1, 0, 2)
        gLay.addWidget(radio_button2, 0, 3)

        self.button_group.buttonClicked.connect(self.file_mode_button_clicked)
        self.fileModeView.setLayout(gLay)

    def init_org_file(self):
        # 源文件选择
        self.orgFileSource = QWidget()
        self.orgFileSource.setWindowTitle('源文件')
        # 源文件选择布局 水平布局
        gLay = QGridLayout()
        gLay.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.orgFileLabel = QLabel('源文件位置：')
        gLay.addWidget(self.orgFileLabel, 0, 0)

        # 地址框
        self.orgPath = QLineEdit()
        self.orgPath.setPlaceholderText('文件地址')
        self.orgPath.setEnabled(False)
        gLay.addWidget(self.orgPath, 0, 1)

        # 文件选择按钮
        button = QPushButton("选择文件")
        button.resize(30, 30)
        button.clicked.connect(lambda: self.on_button_clicked(self.orgPath))
        gLay.addWidget(button, 0, 8)

        gLay.setColumnMinimumWidth(0, 100)
        gLay.setColumnMinimumWidth(1, 500)
        self.orgFileSource.setLayout(gLay)

    def on_button_clicked(self, lab: QLineEdit):
        file_dialog = QFileDialog()
        file_path = None
        if self.fileMode == QFileDialog.FileMode.AnyFile:
            file_path, _ = file_dialog.getOpenFileName(self, "选择文件")
        elif self.fileMode == QFileDialog.FileMode.Directory:
            file_path = file_dialog.getExistingDirectory(self, "选择文件夹")
        if file_path:
            file_info = QFileInfo(file_path)
            lab.setText(file_info.absoluteFilePath())

    def file_mode_button_clicked(self, button):
        print(f"按钮被点击：{button.text()}")
        if button.text() == '同步文件':
            self.fileMode = QFileDialog.FileMode.AnyFile
        else:
            self.fileMode = QFileDialog.FileMode.Directory

    def init_backup_file(self):
        # 源文件选择
        self.backUpFileSource = QWidget()
        self.backUpFileSource.setWindowTitle('备份文件')
        # 源文件选择布局 水平布局
        gLay = QGridLayout()
        gLay.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.backUpFileLabel = QLabel('备份文件位置：')
        gLay.addWidget(self.backUpFileLabel, 0, 0)

        # 地址框
        self.backUpPath = QLineEdit()
        self.backUpPath.setPlaceholderText('文件地址')
        self.backUpPath.setEnabled(False)
        gLay.addWidget(self.backUpPath, 0, 1)

        # 文件选择按钮
        button = QPushButton("选择文件")
        button.clicked.connect(lambda: self.on_button_clicked(self.backUpPath))
        gLay.addWidget(button, 0, 8)

        gLay.setColumnMinimumWidth(0, 100)
        gLay.setColumnMinimumWidth(1, 500)
        self.backUpFileSource.setLayout(gLay)
        pass
